<?php
$db_connect = mysqli_connect("localhost", "root", "", "battleofthemages");
session_start();
require_once("User.php");
require_once("Battle.php");

if($_POST['action'] == 'challenger'){
    $challenger = unserialize($_SESSION['user']);
    $challengerUsername = $challenger->getUsername();
    $challengedUsername = $_POST['username'];
    echo $challengerUsername;
    echo $challengedUsername;

    $idSelectChallengerQuery = "SELECT username, user_id FROM user WHERE username=\"$challengerUsername\"";
    $challengerQueryResult = mysqli_query($db_connect, $idSelectChallengerQuery);
    $challengerRow = mysqli_fetch_assoc($challengerQueryResult);
    $challengerId = $challengerRow["user_id"];
    echo $challengerId;

    $idSelectchallengedQuery = "SELECT username, user_id FROM user WHERE username=\"$challengedUsername\"";
    $challengedQueryResult = mysqli_query($db_connect, $idSelectchallengedQuery);
    $challengedRow = mysqli_fetch_assoc($challengedQueryResult);
    $challengedId = $challengedRow['user_id'];
    echo $challengedId;

    $query = "INSERT INTO battle(user_one, user_two, status) VALUES (\"$challengerId\",\"$challengedId\",\"challenged\")";
    mysqli_query($db_connect, $query);
}else if($_POST['action'] == "challenged"){
    $user = unserialize($_SESSION['user']);
    $username = $user->getUsername();

    $idSelectchallengedQuery = "SELECT username, user_id FROM user WHERE username=\"$username\"";
    $challengedQueryResult = mysqli_query($db_connect, $idSelectchallengedQuery);
    $challengedRow = mysqli_fetch_assoc($challengedQueryResult);
    $challengedId = $challengedRow['user_id'];

    $query = "SELECT user_one, user_two,status FROM battle WHERE user_two=\"$challengedId\" AND status=\"challenged\"";
    $result = mysqli_query($db_connect, $query);
    if(mysqli_num_rows($result) > 0){
        echo "challenged";
    }else{
        echo "not";
    }
}else if($_POST['action'] == "decline"){
    $user = unserialize($_SESSION['user']);
    $username = $user->getUsername();
    $idSelectQuery = "SELECT username, user_id FROM user WHERE username=\"$username\"";
    $queryResult = mysqli_query($db_connect, $idSelectQuery);
    $row = mysqli_fetch_assoc($queryResult);
    $id = $row['user_id'];

    $declineQuery = "UPDATE battle SET status=\"declined\" WHERE user_two=$id";
    mysqli_query($db_connect, $declineQuery);
}else if($_POST['action'] == "accept"){
    $user = unserialize($_SESSION['user']);
    $username = $user->getUsername();
    $idSelectQuery = "SELECT username, user_id FROM user WHERE username=\"$username\"";
    $queryResult = mysqli_query($db_connect, $idSelectQuery);
    $row = mysqli_fetch_assoc($queryResult);
    $id = $row['user_id'];

    $declineQuery = "UPDATE battle SET status=\"accepted\" WHERE user_two=$id";
    mysqli_query($db_connect, $declineQuery);

    $battleIdQuery = "SELECT battle_id, user_one, user_two FROM battle WHERE user_two=$id";
    $result = mysqli_query($db_connect, $battleIdQuery);
    if(mysqli_num_rows($result) > 0){
        $row = mysqli_fetch_assoc($result);

        $battle = new Battle($row['battle_id'], $row['user_one'], $row['user_two']);
        $_SESSION['battle'] = serialize($battle);

        $_SESSION['opponent'] = $row['user_one'];

        $firstRoundQuery = "INSERT INTO round(battle_id, user_one_field, user_two_field, user_one_hand, user_two_hand, user_one_hp, user_two_hp, user_one, user_two, turn) VALUES (". $row['battle_id'] . ",\"\",\"\",\"\",\"\",100,100," . $row['user_one']. "," . $row['user_two'] . ",".$row['user_one'].")";
        mysqli_query($db_connect, $firstRoundQuery);
        echo $row['battle_id'];
    }


}else if($_POST['action'] == "check_status"){
    $user = unserialize($_SESSION['user']);
    $username = $user->getUsername();
    $idSelectQuery = "SELECT username, user_id FROM user WHERE username=\"$username\"";
    $queryResult = mysqli_query($db_connect, $idSelectQuery);
    $row = mysqli_fetch_assoc($queryResult);
    $id = $row['user_id'];

    $checkAcceptedQuery = "SELECT battle_id, user_one, user_two, status FROM battle WHERE (user_one=$id OR user_two=$id) AND status=\"accepted\"";
    $checkAcceptedResult = mysqli_query($db_connect, $checkAcceptedQuery);
    if(mysqli_num_rows($checkAcceptedResult) > 0){
        $row = mysqli_fetch_assoc($checkAcceptedResult);
        echo $row['battle_id'];

        $battle = new Battle($row['battle_id'], $row['user_one'], $row['user_two']);
        $_SESSION['battle'] = serialize($battle);
    }

    $checkDeclinedQuery = "SELECT battle_id, user_one, user_two, status FROM battle WHERE user_one=$id AND status=\"declined\"";
    $checkDeclinedResult = mysqli_query($db_connect, $checkDeclinedQuery);
    if(mysqli_num_rows($checkDeclinedResult) > 0){
        $deleteBattleQuery = "DELETE FROM battle WHERE user_one=$id AND status=\"declined\"";
        mysqli_query($db_connect, $deleteBattleQuery);
    }
}


mysqli_close($db_connect);
?>